package com.cloudsigns.factorymanager2.engineering.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;

import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/1/14.
 */

public class WattingToReceiveEngineeringOrderAdapter extends BaseAdapter {

    private Context context;
    private List<OrderParentBean> datas = new ArrayList<OrderParentBean>();
    private LayoutInflater inflater;
    private ItemClickListener clickListener;
    private CheckedChangeListener checkedChangeListener;
    private String TAG = "";

    private int selectedPosition = -1;


    public WattingToReceiveEngineeringOrderAdapter(Context context, String TAG) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.TAG = TAG;
    }

    public void setData(List<OrderParentBean> list) {
        this.datas.clear();
        if (null != list) {
            this.datas.addAll(list);
        }

    }

    public void setItemClick(ItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setCheckedChangeListener(CheckedChangeListener checkedChangeListener) {
        this.checkedChangeListener = checkedChangeListener;
    }

    public void setSelectedPosition(int selectedPosition) {
        this.selectedPosition = selectedPosition;
    }

    public int getSelectedPosition() {
        return selectedPosition;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public OrderParentBean getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.item_watting_to_receiv_engineering_order_lv, null);
            holder.tv_top = convertView.findViewById(R.id.tv_top);
            holder.tv_bottom = convertView.findViewById(R.id.tv_bottom);
            holder.tv_status = convertView.findViewById(R.id.tv_status);
            holder.cb = convertView.findViewById(R.id.cb);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (TAG.equals("HasBeenReceivedFragment")) {
           // holder.cb.setVisibility(View.VISIBLE);
        }

        holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b && selectedPosition != position) {
                    selectedPosition = position;

                    if (checkedChangeListener != null) {
                        checkedChangeListener.checkedChange();
                    }


                    notifyDataSetChanged();
                } else if (!b && selectedPosition == position) {
                    selectedPosition = -1;

                    if (checkedChangeListener != null) {
                        checkedChangeListener.checkedChange();
                    }

                    notifyDataSetChanged();
                }


            }
        });

        if (selectedPosition == position) {
            holder.cb.setChecked(true);
        } else {
            holder.cb.setChecked(false);
        }

        OrderParentBean bean = datas.get(position);
        if (null != bean.getCategory()) {
            CommonEnum.EngineeringORderCategory category = bean.getCategory();
            if (category == CommonEnum.EngineeringORderCategory.INSTALL) {
                holder.tv_top.setText("工程安装");
                holder.tv_bottom.setText(bean.getProjectName() + "/" + bean.getEngineeringName() + "/" + DateUtil.getYyyy_Mm_Dd(bean.getCreateDate()));

            } else if (category == CommonEnum.EngineeringORderCategory.BATCH_INSTALL) {
                holder.tv_top.setText("批量安装");
                holder.tv_bottom.setText(bean.getProjectName() + "/" + bean.getEngineeringName() + "/" + DateUtil.getYyyy_Mm_Dd(bean.getCreateDate()));

            } else if (category == CommonEnum.EngineeringORderCategory.CONSTRUCT) {
                holder.tv_top.setText("工程施工");
                holder.tv_bottom.setText(bean.getProjectName() + "/" + bean.getEngineeringName() + "/" + DateUtil.getYyyy_Mm_Dd(bean.getCreateDate()));

            } else if (category == CommonEnum.EngineeringORderCategory.MAINTAIN) {
                holder.tv_top.setText("维修");
                holder.tv_bottom.setText(bean.getProjectName() + "/" + bean.getEngineeringName() + "/" + DateUtil.getYyyy_Mm_Dd(bean.getCreateDate()));

            } else if (category == CommonEnum.EngineeringORderCategory.PUBLICATION) {
                holder.tv_top.setText("广告上刊");
                holder.tv_bottom.setText(bean.getProjectName() + "/" + bean.getEngineeringName() + "/" + DateUtil.getYyyy_Mm_Dd(bean.getCreateDate()));

            } else if (category == CommonEnum.EngineeringORderCategory.PATROL) {
                holder.tv_top.setText("巡查");
                holder.tv_bottom.setText(bean.getProjectName() + "/" + DateUtil.getYyyy_Mm_Dd(bean.getCreateDate()));
            } else if (category == CommonEnum.EngineeringORderCategory.SURVEY) {
                holder.tv_top.setText("现场勘察");
                holder.tv_bottom.setText(bean.getProjectName() + "/" + DateUtil.getYyyy_Mm_Dd(bean.getCreateDate()));
            }


        }

        if (TAG.equals("HasBeenReceivedFragment")) {
            if (bean.isHasPauseOrder()) {
                holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_00B050));
                holder.tv_status.setText("[暂停]");
            } else {
                holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_00B050));
                holder.tv_status.setText("");
            }

        }

//        holder.tv_bottom.setText(bean.getProjectName() + "/" + bean.getEngineeringName() + "/" + DateUtil.getYyyy_Mm_Dd(bean.getCreateDate()));


        return convertView;
    }

    private class ViewHolder {
        CheckBox cb;
        private TextView tv_top;
        private TextView tv_bottom;
        private TextView tv_status;

    }

    public interface CheckedChangeListener {
        public void checkedChange();
    }
}
